#coding:utf-8
'''
create on 2017-07-22
@author:sandy
'''
import logging

class Card(object):
    
    def __init__(self):
        self.index = 0
        self.sid = 0
        self.cid = 0
        self.info = u''

    def parse(self,cf):
        self.index = cf.get("index")
        self.sid = cf.get("sid")
        self.cid = cf.get("cid")
        self.info = cf.get("info")
        self.check()
        logging.debug("%d--->%s",self.sid,self.info)
        
    def check(self):
        t = self.sid / 100
        v = self.sid % 10
        if (t - 1) * 10 + v != self.cid:
            logging.error("index=%d config error",self.index)
 
class MjConfig(object):
    
    def __init__(self):
        self.__cardMap = {}

    def onRemoteConfig(self, cfgname, jf):
        if cfgname!= u"clientconfig.mjconfig":
            return
        self.loadFromJson(jf)
    
    def loadFromJson(self, cf):
        logging.debug("onRemoteConfig clientconfig.mjconfig parse")
        cards = cf.get("cards")
        logging.debug("cards = %d",len(cards))
        for i in range(len(cards)):
            c = Card()
            c.parse(cards[i])
            self.__cardMap[c.sid] = c

    def getCardIds(self):
        cardids = []
        for card in self.__cardMap:
            cardids.append(card.get("sid"))
        return cardids


    def getCidBySid(self,sid):
        cid = 0
        card = self.__cardMap.get(sid,None)
        if card:
            cid = card.cid
        return cid

    def getCidsBySids(self,sids):
        cids = []
        for sid in sids:
            cids.append(self.getCidBySid(sid))
        return cids

    
    
instance = MjConfig()

if __name__ == '__main__':
    pass